﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using Demo_G.O.S.E.ServerEngine.Common;
using System.Threading;
using Demo_G.O.S.E.ServerEngine.Util;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Ability.Spell
{
    /// <summary>
    /// 
    /// </summary>
    public class BaseSpellManager
    {
        #region zh-CHS 共有属性 | en Public Properties
        /// <summary>
        /// 给出脚本里面的可运动物体的数量(NPCs、Monster)
        /// </summary>
        public int Count
        {
            get { return m_Spells.Count; }
        }
        #endregion

        #region zh-CHS SpellManager方法 | en Public Methods

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private SafeDictionary<long, BaseSpell> m_Spells = new SafeDictionary<long, BaseSpell>();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseTaxiNode"></param>
        public bool AddSpell( long iSpellSerial, BaseSpell baseSpell )
        {
            if ( baseSpell == null )
            {
                Debug.WriteLine( "BaseSpellManager.AddSpell(...) - baseSpell == null error!" );
                return false;
            }

            EventHandler<BeforeAddSpellCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeAddSpellCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeAddSpellCallEventArgs eventArgs = new BeforeAddSpellCallEventArgs( baseSpell, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_Spells.Add( iSpellSerial, baseSpell );

            EventHandler<AfterAddSpellCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterAddSpellCall;
            if ( tempAfterEventArgs != null )
            {
                AfterAddSpellCallEventArgs eventArgs = new AfterAddSpellCallEventArgs( baseSpell, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public BaseSpell FindSpellOnSerial( long iSpellSerial )
        {
            return m_Spells.GetValue( iSpellSerial );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool RemoveSpell( long iSpellSerial )
        {
            BaseSpell baseSpell = FindSpellOnSerial( iSpellSerial );
            if ( baseSpell == null )
                return false;

            EventHandler<BeforeRemoveSpellCallEventArgs> tempBeforeEventArgs = m_ThreadEventBeforeRemoveSpellCall;
            if ( tempBeforeEventArgs != null )
            {
                BeforeRemoveSpellCallEventArgs eventArgs = new BeforeRemoveSpellCallEventArgs( baseSpell, this );
                tempBeforeEventArgs( this, eventArgs );

                if ( eventArgs.IsCancel == true )
                    return false;
            }

            m_Spells.Remove( iSpellSerial );

            EventHandler<AfterRemoveSpellCallEventArgs> tempAfterEventArgs = m_ThreadEventAfterRemoveSpellCall;
            if ( tempAfterEventArgs != null )
            {
                AfterRemoveSpellCallEventArgs eventArgs = new AfterRemoveSpellCallEventArgs( baseSpell, this );
                tempAfterEventArgs( this, eventArgs );
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        [MultiThreadedWarningAttribute( "(Spell)当前的数组是列表临时产生的,不能保存数组用于以后操作:警告!" )]
        public BaseSpell[] ToArray()
        {
            return m_Spells.ToArray();
        }

        #endregion

        #region zh-CHS 共有事件 | en Public Event

        #region zh-CHS AddSpellCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeAddSpellCallEventArgs> m_ThreadEventBeforeAddSpellCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeAddSpellCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeAddSpellCallEventArgs> ThreadBeforeAddSpellCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddSpellCall );
                {
                    m_ThreadEventBeforeAddSpellCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddSpellCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeAddSpellCall );
                {
                    m_ThreadEventBeforeAddSpellCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeAddSpellCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterAddSpellCallEventArgs> m_ThreadEventAfterAddSpellCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterAddSpellCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterAddSpellCallEventArgs> ThreadAfterAddSpellCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddSpellCall );
                {
                    m_ThreadEventAfterAddSpellCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddSpellCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterAddSpellCall );
                {
                    m_ThreadEventAfterAddSpellCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterAddSpellCall );
            }
        }
        #endregion

        #region zh-CHS RemoveSpellCall事件 | en Public Event
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<BeforeRemoveSpellCallEventArgs> m_ThreadEventBeforeRemoveSpellCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventBeforeRemoveSpellCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<BeforeRemoveSpellCallEventArgs> ThreadBeforeRemoveSpellCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveSpellCall );
                {
                    m_ThreadEventBeforeRemoveSpellCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveSpellCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventBeforeRemoveSpellCall );
                {
                    m_ThreadEventBeforeRemoveSpellCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventBeforeRemoveSpellCall );
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private EventHandler<AfterRemoveSpellCallEventArgs> m_ThreadEventAfterRemoveSpellCall;
        /// <summary>
        /// 
        /// </summary>
        private ReaderWriterLockSlim m_LockThreadEventAfterRemoveSpellCall = new ReaderWriterLockSlim();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<AfterRemoveSpellCallEventArgs> ThreadAfterRemoveSpellCall
        {
            add
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveSpellCall );
                {
                    m_ThreadEventAfterRemoveSpellCall += value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveSpellCall );
            }
            remove
            {
                ReaderWriterLockSlimEx.EnterWriteLock( m_LockThreadEventAfterRemoveSpellCall );
                {
                    m_ThreadEventAfterRemoveSpellCall -= value;
                }
                ReaderWriterLockSlimEx.ExitWriteLock( m_LockThreadEventAfterRemoveSpellCall );
            }
        }
        #endregion

        #endregion
    }
}
#endregion